package game

import (
	"context"
	"qm_system_server/server/global"
	"qm_system_server/server/model/common/response"
	"qm_system_server/server/proto/grpc/game"

	"github.com/gin-gonic/gin"
	"github.com/spf13/cast"
	"google.golang.org/grpc/status"
)

type QmGameApi struct {
}

// CreateQmGame 创建游戏记录
// @Tags QmGame
// @Summary 创建游戏记录
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body game.QmGame true "创建游戏记录"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"创建成功"}"
// @Router /qmGame/createQmGame [post]
func (qmGameApi *QmGameApi) CreateQmGame(c *gin.Context) {
	type request struct {
		GameName    string `json:"game_name" form:"game_name"`       // 游戏名称
		Type        string `json:"type" form:"type"`                 // 游戏类型
		DevelopType int64  `json:"develop_type" form:"develop_type"` // 开发类型
		CpName      string `json:"cp_name" form:"cp_name"`           // 厂商名称
		OriginName  string `json:"origin_name" form:"origin_name"`   // 来源名称
	}
	var req request
	_ = c.ShouldBindJSON(&req)

	if req.GameName == "" || req.DevelopType == 0 || req.OriginName == "" || req.Type == "" {
		response.FailWithMessage("参数不能为空", c)
		return
	}

	if req.DevelopType == 2 {
		if req.CpName == "" {
			response.FailWithMessage("厂商名称不能为空", c)
			return
		}
	}

	client := game.NewGameServiceClient(global.ServerGrpc)

	_, err := client.Create(context.Background(), &game.CreateRequest{
		Game: &game.Game{
			GameName:    req.GameName,
			Type:        req.Type,
			DevelopType: req.DevelopType,
			CpName:      req.CpName,
			OriginName:  req.OriginName,
		},
	})

	if err != nil {
		st, ok := status.FromError(err)
		if ok {
			// 获取 gRPC 错误的描述信息
			response.FailWithMessage(st.Message(), c)
		}
		return
	}
	response.OkWithMessage("创建成功", c)
}

// DeleteQmGame 删除游戏记录
// @Tags QmGame
// @Summary 删除游戏记录
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body game.QmGame true "删除游戏记录"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"删除成功"}"
// @Router /qmGame/deleteQmGame [delete]
func (qmGameApi *QmGameApi) DeleteQmGame(c *gin.Context) {
	ID := c.Query("id")

	//转成int64
	id := cast.ToInt64(ID)

	client := game.NewGameServiceClient(global.ServerGrpc)

	_, err := client.Delete(context.Background(), &game.DeleteRequest{
		Id: id,
	})

	if err != nil {
		st, ok := status.FromError(err)
		if ok {
			// 获取 gRPC 错误的描述信息
			response.FailWithMessage(st.Message(), c)
		}
		return
	}
	response.OkWithMessage("删除成功", c)
}

// UpdateQmGame 更新游戏记录
// @Tags QmGame
// @Summary 更新游戏记录
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body game.QmGame true "更新游戏记录"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"更新成功"}"
// @Router /qmGame/updateQmGame [put]
func (qmGameApi *QmGameApi) UpdateQmGame(c *gin.Context) {
	type request struct {
		Id          int64  `json:"id" form:"id"`
		GameName    string `json:"game_name" form:"game_name"`       // 游戏名称
		Type        string `json:"type" form:"type"`                 // 游戏类型
		DevelopType int64  `json:"develop_type" form:"develop_type"` // 开发类型
		CpName      string `json:"cp_name" form:"cp_name"`           // 厂商名称
		OriginName  string `json:"origin_name" form:"origin_name"`   // 来源名称
	}

	var req request

	_ = c.ShouldBindJSON(&req)

	if req.Id == 0 || req.GameName == "" || req.DevelopType == 0 || req.OriginName == "" || req.Type == "" {
		response.FailWithMessage("参数不能为空", c)
		return
	}

	if req.DevelopType == 2 {
		if req.CpName == "" {
			response.FailWithMessage("厂商名称不能为空", c)
			return
		}
	}

	client := game.NewGameServiceClient(global.ServerGrpc)

	_, err := client.Update(context.Background(), &game.UpdateRequest{
		Game: &game.Game{
			Id:          req.Id,
			GameName:    req.GameName,
			DevelopType: req.DevelopType,
			CpName:      req.CpName,
			OriginName:  req.OriginName,
			Type:        req.Type,
		},
	})

	if err != nil {
		st, ok := status.FromError(err)
		if ok {
			// 获取 gRPC 错误的描述信息
			response.FailWithMessage(st.Message(), c)
		}
		return
	}

	response.OkWithMessage("更新成功", c)
}

// FindQmGame 用id查询游戏记录
// @Tags QmGame
// @Summary 用id查询游戏记录
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query game.QmGame true "用id查询游戏记录"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"查询成功"}"
// @Router /qmGame/findQmGame [get]
func (qmGameApi *QmGameApi) FindQmGame(c *gin.Context) {
	type request struct {
		Id int64 `json:"id" form:"id"`
	}

	var req request

	c.ShouldBindQuery(&req)

	//判断不能为空
	if req.Id == 0 {
		response.FailWithMessage("参数不能为空", c)
		return
	}

	client := game.NewGameServiceClient(global.ServerGrpc)
	getResp, err := client.Get(context.Background(), &game.GetRequest{
		Id: req.Id,
	})

	if err != nil {
		st, ok := status.FromError(err)
		if ok {
			// 获取 gRPC 错误的描述信息
			response.FailWithMessage(st.Message(), c)
		}
		return
	}

	response.OkWithData(getResp.Game, c)
}

// GetQmGameList 分页获取游戏记录列表
// @Tags QmGame
// @Summary 分页获取游戏记录列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query game.QmGameSearch true "分页获取游戏记录列表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /qmGame/getQmGameList [get]
func (qmGameApi *QmGameApi) GetQmGameList(c *gin.Context) {
	type request struct {
		Page        int64  `json:"page" form:"page"`
		PageSize    int64  `json:"page_size" form:"page_size"`
		GameName    string `json:"game_name" form:"game_name"`
		DevelopType int64  `json:"develop_type" form:"develop_type"` // 开发类型
		CpName      string `json:"cp_name" form:"cp_name"`           // 厂商名称
		OriginName  string `json:"origin_name" form:"origin_name"`   // 来源名称
		Type        string `json:"type" form:"type"`
	}

	var req request

	_ = c.ShouldBindQuery(&req)

	client := game.NewGameServiceClient(global.ServerGrpc)

	games, err := client.List(context.Background(), &game.ListRequest{
		Page:        req.Page,
		PageSize:    req.PageSize,
		GameName:    req.GameName,
		DevelopType: req.DevelopType,
		CpName:      req.CpName,
		OriginName:  req.OriginName,
		Type:        req.Type,
	})

	if err != nil {
		st, ok := status.FromError(err)
		if ok {
			// 获取 gRPC 错误的描述信息
			response.FailWithMessage(st.Message(), c)
		}
		return
	}

	response.OkWithData(response.PageResult{
		List:  games.Game,
		Total: games.Total,
		Page:  int(req.Page),
	}, c)
}
